package com.yvan.leto.server.controller;

import com.yvan.leto.ResourceFoundException;
import com.yvan.leto.SegmentHelper;
import com.yvan.leto.server.config.LetoServerProperties;
import com.yvan.leto.server.service.ApiService;
import com.yvan.leto.server.service.ReleaseService;
import com.yvan.leto.server.utils.HttpUtil;
import com.yvan.leto.server.utils.StringView;
import com.yvan.platform.JsonWapper;
import com.yvan.platform.ResultModel;
import com.yvan.platform.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by luoyifan on 2018/6/11.
 */
@RestController
public class ApiController {

    @Autowired
    private LetoServerProperties letoServerProperties;

    @Autowired
    private ApiService apiService;

    @Autowired
    private ReleaseService releaseService;

    @Value("${leto_server.publish-zk:}")
    public String publishZk;

    @GetMapping("/leto/api/zk")
    public Object getZk() {
        return new ResultModel()
                .set("server-list", (StringUtils.isNullOrEmpty(publishZk) ? letoServerProperties.getServerList() : publishZk))
                .set("namespace", letoServerProperties.getNamespace());
    }

    @RequestMapping(value = "/leto/dbg/{env}/{project}/{segment}", produces = {}, method = RequestMethod.GET)
    public ModelAndView debugSegment(@PathVariable("project") String projectName,
                                     @PathVariable("env") String env,
                                     @PathVariable("segment") String segment,
                                     HttpServletResponse rsp) throws IOException {
        try {
            String content = apiService.debugMe(env, projectName, segment);
            return new ModelAndView(new StringView(content, SegmentHelper.getHttpContentType(segment) + ";charset=utf-8"));

        } catch (ResourceFoundException e) {
            HttpUtil.currentResponse().sendError(404, e.getMessage());
            return null;
        }
    }

    @RequestMapping(value = "/leto/diff/{env}/{project}/{segment}", produces = {}, method = RequestMethod.GET)
    public ModelAndView diffSegment(@PathVariable("project") String projectName,
                                    @PathVariable("env") String env,
                                    @PathVariable("segment") String segment,
                                    HttpServletResponse rsp) throws IOException {

        JsonWapper jwContent = new JsonWapper();
        jwContent.set("value", apiService.debugMe(env, projectName, segment));
        if (releaseService.existsResult(env, projectName, segment)) {
            jwContent.set("orig", apiService.service(env, projectName, segment));
        } else {
            jwContent.set("orig", "");
        }

        ResultModel model = new ResultModel()
                .set("project", projectName)
                .set("env", env)
                .set("segment", segment)
                .set("content", jwContent.toString(false));
        return new ModelAndView("/diff.ftl", model);
    }

    @RequestMapping(value = "/leto/api/{env}/{project}/{segment}", produces = {}, method = RequestMethod.GET)
    public ModelAndView letoApiEnvProjectSegment(@PathVariable("project") String projectName,
                                                 @PathVariable("env") String env,
                                                 @PathVariable("segment") String segment,
                                                 HttpServletResponse rsp) throws IOException {
        try {
            String content = apiService.service(env, projectName, segment);
            return new ModelAndView(new StringView(content, SegmentHelper.getHttpContentType(segment) + ";charset=utf-8"));

        } catch (ResourceFoundException e) {
            HttpUtil.currentResponse().sendError(404, e.getMessage());
            return null;
        }
    }
}
